;(function(){

    class AddCartGoods{
        constructor(){
            this.addCartBtn = $("#addBtn") ;
            // 默认处理
            this.goodsid=location.search.split("=")[1];
            
            if( this.goodsid!="a00001" || this.goodsid !="a00022"){
               this.goodsid=this.goodsid;
            }else{
                this.goodsid="a00001";
            }
        }
        getData(){
            // 获取用户名
            if(JSON.parse(sessionStorage.getItem("data"))){
                this.user = JSON.parse(sessionStorage.getItem("data"))[0].user;
            }
            // 获取商品数据
            pAjax({
                url:"http://localhost/XMYP/src/php/cart.php",
                type:"get",
                data:{
                    user:this.user,
                }
            }).then((res)=>{
                this.goodsData = JSON.parse(res);
                if(this.goodsData.code==1){
                    // 没有数据情况为1
                    this.judge = 1;
                }else if(!this.goodsData.code){
                    // 有数据情况为2
                    this.judge = 2;
                }
            });
            // 获取商品详情页数据
            var that =this;
            pAjax({
                type:"get",
                url:"http://localhost/XMYP/src/php/details.php",
                data:{
                    details:this.goodsid,
                }
            }).then(function(res){
                that.detailData = JSON.parse(res);
            });
        }
        addEvent(){
            this.addCartBtn.on("click",()=>{
                this.judgeData();

            });
        }
        judgeData(g){
            // 默认处理
            // goodsid=location.search
            // var goodsid = g.substr(1);
            // goodsid = goodsid.split("=");
            // this.goodsid = goodsid[1];

            if(this.judge==1){
                this.addData();
            }else if(this.judge==2){
                // 判断是否有相同的商品id，默认没有
                var k = 0;
                // 判断第几个相同；默认为999
                var t = 999;
                for(let i = 0 ; i < this.goodsData.length ;i++){
                    if(this.goodsid == this.goodsData[i].goodsid){
                        // 如果有相同的传1
                        k = 1 ;
                        t = i;
                    }
                }
                this.addData(k,t);
            }
        }
        addData(k,t){
            if(k==0){
                // 默认处理img地址
                var img = "";
                var shop="";
                if(this.goodsid=="a00001"){
                    img = "http://localhost/XMYP/src/static/img/goods01.webp";
                    shop = "Apple";
                }else{
                    img = "https://img.youpin.mi-img.com/800_pic/f3d60dd9eae998425b954399d6a48387.png@base@tag=imgScale&F=webp";
                    shop = "有道精选";
                };
                pAjax({
                    url:"http://localhost/XMYP/src/php/insert.php",
                    data:{
                        price:this.detailData[0].price,
                        goodsMsg:this.detailData[0].title,
                        num:1,
                        user:this.user,
                        goodsid: this.goodsid,
                        img:img,
                        shop:shop,
                    }
                }).then((res)=>{
                    console.log(res);
                })
            }else if(k==1){
                pAjax({
                    url:"http://localhost/XMYP/src/php/updata.php", 
                    data:{
                        user:this.user,
                        goodsid: this.goodsid,
                        num:Number(this.goodsData[t].num)+1,
                    }
                }).then((r)=>{
                    this.addEle();
                })
            };   
        }
        addEle(){
            var div = $("<div class='addCartSuccess'>加入购物车成功</div>");
            $(".search").prepend(div);
            setInterval(() => {
                $(".addCartSuccess").css("display","none");
            }, 3000);
        }
    }

    let addCartGoods = new AddCartGoods();
    addCartGoods.getData();
    addCartGoods.addEvent();



// price:2499,
// goodsMsg:a00001,
// num:1,
// user:13428098775,
// goodsid:a00001,
// img:http://localhost/XMYP/src/static/img/goods01.webp,
// shop:Apple,








}());